package es.udc.emarketapp.http.controller.actions.sellers;


import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.emarketapp.emarket.model.seller.vo.SellerVO;
import es.udc.emarketapp.emarket.model.usersFacade.UsersFacade;
import es.udc.emarketapp.emarket.model.usersFacade.util.PasswordEncrypter;
import es.udc.emarketapp.emarket.util.exceptions.model.NifCifAlreadyExistsException;
import es.udc.emarketapp.emarket.util.exceptions.model.UserNameAlreadyExistsException;
import es.udc.emarketapp.http.view.actionForms.RegisterSellerForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;


public class RegisterSellerAction extends DefaultAction {

    final static String SELLER = "SELLER";

    public ActionForward doExecute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */

        RegisterSellerForm regsterSellerForm = (RegisterSellerForm) form;
        String idSeller = regsterSellerForm.getIdSeller();
        String pass = regsterSellerForm.getPass();
        String retypePass = regsterSellerForm.getRetypePass();
        String name = regsterSellerForm.getName();
        String surname1 = regsterSellerForm.getSurname1();
        String surname2 = regsterSellerForm.getSurname2();
        String nifCif = regsterSellerForm.getNifCif();
        String email = regsterSellerForm.getEmail();
        String tlf1 = regsterSellerForm.getTlf1();
        String tlf2 = regsterSellerForm.getTlf2();
        String address = regsterSellerForm.getAddress();
        String city = regsterSellerForm.getCity();
        String cp = regsterSellerForm.getCp();
        String province = regsterSellerForm.getProvince();
        String country = regsterSellerForm.getCountry();

        String nomEmp = regsterSellerForm.getNomEmp();
        String nRegMerc = regsterSellerForm.getnRegMerc();
        String fax = regsterSellerForm.getFax();
        String iban = regsterSellerForm.getIban();
        String swift = regsterSellerForm.getSwift();

        /* Create client*/
        ActionMessages errors = new ActionMessages();
        UsersFacade usersFacade = new UsersFacade();

        /* Crypt pass */
        String encryptedPass = PasswordEncrypter.crypt(pass);

        SellerVO sellerVO = new SellerVO(idSeller, encryptedPass, name, surname1,
                surname2, nifCif, address, city, cp, province, country, email,
                tlf1, tlf2, SELLER, true, true, nomEmp, nRegMerc, fax, 
                iban, swift);

        /*
         * Insert the SellerVO in the database, and the returned SellerVO
         * (which contains the client identifier) in the request.
         */

        try {

            SellerVO insertedSellerVO = usersFacade.createSeller(sellerVO);

            request.setAttribute("seller", insertedSellerVO);
            

        } catch (InternalErrorException e) {
            System.out.println("Error al registrar vendedor " + e);
        } catch (UserNameAlreadyExistsException e) {
            errors.add("idSeller",
                    new ActionMessage("ErrorMessages.idSeller.alreadyExists"));
        } catch (NifCifAlreadyExistsException e) {
            errors.add("nifCif",
                    new ActionMessage("ErrorMessages.nifCif.alreadyExists"));
        }
        /* Return ActionForward. */
        if (errors.isEmpty()) {
            return mapping.findForward("Success");
        } else {
            saveErrors(request, errors);
            return new ActionForward(mapping.getInput());
        }
    }
}
